package com.zhouyibike.uu.ui.activity.youhuihuodong;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.CheckPromoCodeResult;
import com.zhouyibike.uu.entity.TwentyActivityResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.mybike.Twentyoffer.Buy20SuccessActivity;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.TextUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HuodongduihuanActivity extends BaseActivity {
    private EditText edittextduihuanma;
    private TextView mBtnTitle;
    private Button btn_logout;
    private List<TwentyActivityResult.DataBean.ImagesBean> imagesBeen = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huodongduihuan);
        initview();
    }
    private void initview() {
//        i.putExtra("shengche20", huodongdanche20);
//        i.putExtra("chong20", (Serializable) imagesBeen);
        imagesBeen = (List<TwentyActivityResult.DataBean.ImagesBean>) getIntent().getSerializableExtra("chong20");
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("输入兑换码");
        edittextduihuanma= (EditText) findViewById(R.id.edittextduihuanma);
        btn_logout= (Button) findViewById(R.id.btn_lijiduihuan);
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isNullorEmpty(edittextduihuanma.getText().toString()))
                {
                    toastShow("活动码不能为空");
                }else{
                    yanzhengCode();
                }
            }
        });
    }
    /**
     * 验证活动码
     */
    private void yanzhengCode() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("promoCode", edittextduihuanma.getText().toString());
      //  showProgressDialog();
        addSubscription(apiStores.CheckPromoCode(params),
                new ApiCallback<CheckPromoCodeResult>() {
                    @Override
                    public void onSuccess(CheckPromoCodeResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            //  toastShow(model.getMessage());
                            setResult(1288);//成功
                            Intent i = new Intent(HuodongduihuanActivity.this, Buy20SuccessActivity.class);
                            i.putExtra("chong20", (Serializable) imagesBeen);
                            startActivity(i);
                            finish();
                        } else {
                            toastShow(model.getMessage());
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
